package com.ruoyi.supply.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.ruoyi.supply.enums.SupplyInfoTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supply.mapper.TInfoImgMapper;
import com.ruoyi.supply.domain.TInfoImg;
import org.springframework.transaction.annotation.Transactional;

/**
 * 货源/地点 咨询图片Service业务层处理
 * 
 * @author Ryan
 * @date 2025-06-05
 */
@Service
public class TInfoImgService extends ServiceImpl<TInfoImgMapper, TInfoImg>
{
    @Autowired
    private TInfoImgMapper tInfoImgMapper;

    public List<String> findImgByType(Long fkInfoId,String type){
        List<TInfoImg> list = lambdaQuery().select(TInfoImg::getUrl)
                .eq(TInfoImg::getFkSupplyInfoId, fkInfoId)
                .eq(TInfoImg::getType, type)
                .eq(TInfoImg::getMiniFlag,false)
                .list();
        if(CollUtil.isEmpty(list)){
            return Collections.emptyList();
        }
        return list.stream().map(TInfoImg::getUrl).collect(Collectors.toList());
    }

    public List<String> findMiniImgByType(Long fkInfoId,String type){
        List<TInfoImg> list = lambdaQuery().select(TInfoImg::getUrl)
                .eq(TInfoImg::getFkSupplyInfoId, fkInfoId)
                .eq(TInfoImg::getType, type)
                .eq(TInfoImg::getMiniFlag,true)
                .list();
        if(CollUtil.isEmpty(list)){
            return Collections.emptyList();
        }
        return list.stream().map(TInfoImg::getUrl).collect(Collectors.toList());
    }
    @Transactional(rollbackFor = Exception.class)
    public void deleteByFkSupplyInfoId(Long fkSupplyInfoId){
        if(Objects.isNull(fkSupplyInfoId)){
            return;
        }
        baseMapper.delete(new QueryWrapper<TInfoImg>().lambda().eq(TInfoImg::getFkSupplyInfoId, fkSupplyInfoId));
    }
    @Transactional(rollbackFor = Exception.class)
    public void deleteByFkSupplyInfoIds(Long[] fkSupplyInfoIds){
        if(Objects.isNull(fkSupplyInfoIds) && fkSupplyInfoIds.length > 0){
            return;
        }
        baseMapper.delete(new QueryWrapper<TInfoImg>().lambda().in(TInfoImg::getFkSupplyInfoId, fkSupplyInfoIds));
    }

    @Transactional(rollbackFor = Exception.class)
    public void batchAddByFkSupplyInfoId(Long fkSupplyInfoId,
                                         List<String> urls,
                                         List<String> miniUrls,
             SupplyInfoTypeEnum typeEnum){

        List<TInfoImg> collect = urls.stream().map(it -> new TInfoImg(null, fkSupplyInfoId, it, typeEnum.getCode(), null, new Date(), null, null,false)).collect(Collectors.toList());
        saveBatch(collect);
        List<TInfoImg> miniCollect = miniUrls.stream().map(it -> new TInfoImg(null, fkSupplyInfoId, it, typeEnum.getCode(), null, new Date(), null, null,true)).collect(Collectors.toList());
        saveBatch(miniCollect);
    }

}
